'use strict';

import ChartUI from '@/components/chartUI.js';

/**
 * @param {Object} e  对应的canvars的id
 * @param {Object} _this
 */
var ChartTimer = function(e, _this) {
	this.interval = 100;
	this.timer = null;
	this.charts = {};
	
	for (let [k, v] of Object.entries(e)) {
		this.charts[k] = new ChartUI(_this, k);
		v(this.charts[k]);
		this.charts[k].render();
		// console.log(this.charts[k]);
	}
};

ChartTimer.prototype.stop = function() {
		clearInterval(this.timer);
};

ChartTimer.prototype.testData = function() {
	for (let [k, v] of Object.entries(this.charts)) {
		for (var j = 0; j < 50000; j++) {
			v.push(Math.random());
		}
	}
	return this;
}

ChartTimer.prototype.setInterval = function(interval) {
	this.interval = interval;
	return this;
};

ChartTimer.prototype.run = function() {
	var _self = this;
	// console.log(_self.charts);
	// return;
	this.timer = setInterval(function() {
		for(let [k, v] of Object.entries(_self.charts)) {
			// v.test();
			// console.log(v.config.series[0]);	
			if (v.dataList.length > 0) {
				v.config.series[0].data.push(v.dataList.shift());
				v.config.series[0].data.shift();
				v.uchart.updateData({
					series: v.config.series,
				});
			}
		}
	},this.interval)
};

if (typeof module === "object" && typeof module.exports === "object") {
	module.exports = ChartTimer;
}